package DataBaseTest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Select extends JInternalFrame {
	DataBaseTest_0528 main;
	DefaultTableModel model;
	Connection conn;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField sql;
	private JScrollPane scrollPane;
	private JTable table;
	private JLabel lblNewLabel;
	

	public Select(Connection c,DataBaseTest_0528 main) {
		super("조회", true, true, true, true);
		this.main=main;
		this.conn = c;
		getContentPane().setBackground(new Color(255, 255, 255));
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		getContentPane().add(getLabel_3(), BorderLayout.SOUTH);
		setMaximizable(true);
		setClosable(true);
		
		setTitle("조회");
		setBounds(100, 100, 500, 300);

	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getSql());
			panel.add(getButton_1(), BorderLayout.EAST);
		}
		return panel;
	}

	public void setList() {
		String s = sql.getText();
		try {
			PreparedStatement pstmt = conn.prepareStatement(s);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector v = new Vector();
			v.add("No.");
			for (int i = 1; i <= rsm.getColumnCount(); i++) {
				v.add(rsm.getColumnName(i));
			}
			model = new DefaultTableModel(v,0);
			table.setModel(model);
			int cnt=1;
			while (rs.next()){
				Vector d= new Vector<>();
				d.add(cnt);
				for(int i=1;i<=rsm.getColumnCount();i++){
					d.add(rs.getString(i));
				}
			model.addRow(d);
			cnt++;
			}
		} catch (Exception e) {
		}
	}

	private JButton getButton_1() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리 실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setList();
				}
			});
		}
		return btnNewButton;
	}

	private JTextField getSql() {
		if (sql == null) {
			sql = new JTextField();
			sql.setColumns(10);
		}
		return sql;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.addMouseListener(new MouseAdapter() {
			
						});
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					if(main.update==null){
						main.openUpdate();
					}
					int row=table.getSelectedRow();
					String s =(String)table.getValueAt(row, 1);
					String c =(String)table.getValueAt(row, 2);
					String ee =(String)table.getValueAt(row, 3);
					
					main.update.setData(s, c, ee);
					main.update.setVisible(true);
					main.update.toFront();//정면에 보이도록
					
					
				}
			});
		}
		return table;
	}

	private JLabel getLabel_3() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("New label");
		}
		return lblNewLabel;
	}
}
